/*
 * Copyright (c) 2022-2023 cetcclout Co., Ltd All rights reserved.
 * Sensor Adapter is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package com.cetccloud.cimp.common.util;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONUtil;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * PM2_5	CO	 PM10	SO2	O3	NO2
 *  51	   0.9	 81 	18	71	28
 *  54	   1	 88	    18	68	30
 */

public class AirSixDataCreator {
	
	private static final Logger log = LoggerFactory.getLogger(AirSixDataCreator.class);
	
    /**
     * 上下浮动20%
     */
    public static String float20(String value){
        double c = RandomUtil.randomDouble(-0.2, 0.2,2, RoundingMode.FLOOR);
        Double dValue = Double.valueOf(value);
        dValue= dValue+dValue*c;
        if(String.valueOf(dValue).contains(".")) {
            String data = String.format("%.2f", Double.valueOf(dValue));
            return data;
        }
        return dValue+"";
    }
    /**
     * 上下浮动20%
     */
    public static String int20(String value){
        double c = RandomUtil.randomDouble(-0.2, 0.2,2, RoundingMode.FLOOR);
        Integer dValue = Integer.valueOf(value);
        dValue= dValue + (int)(dValue*c);
        return dValue+"";
    }


    /**
     * PM2_5	CO	PM10	SO2	O3	NO2
     * @param deviceId
     * @param dataBlock
     * @param randomData
     */
    public static void createAirRtdData(String deviceId, StringBuilder dataBlock, Map<String, Object> randomData) {
        
        String SO2D = float20(randomData.get("SO2").toString());
        String NO2D = float20(randomData.get("NO2").toString());
        String COD = float20(randomData.get("CO").toString());
        String O3D = float20(randomData.get("O3").toString());
        String PM10D = float20(randomData.get("PM10").toString());
        String PM25D = float20(randomData.get("PM2_5").toString());

        //SO2
        dataBlock.append("a21026-Rtd="+ SO2D +",a21026-Flag=N;");
        //NO2
        dataBlock.append("a21004-Rtd="+ NO2D +",a21004-Flag=N;");
        //CO
        dataBlock.append("a21005-Rtd="+ COD +",a21005-Flag=N;");
        //O3
        dataBlock.append("a05024-Rtd="+ O3D +",a05024-Flag=N;");
        //PM10
        dataBlock.append("a34002-Rtd="+ PM10D +",a34002-Flag=N;");
        //PM2.5
        dataBlock.append("a34004-Rtd="+ PM25D +",a34004-Flag=N");


        List<Double> SO2 = DataConfig.MINUTE_DATA.get(deviceId,"a21026")==null?new ArrayList<>():DataConfig.MINUTE_DATA.get(deviceId,"a21026");
        SO2.add(Double.valueOf(SO2D));
        DataConfig.MINUTE_DATA.put(deviceId, "a21026", SO2);

        List<Double> NO2 = DataConfig.MINUTE_DATA.get(deviceId,"a21004")==null?new ArrayList<>():DataConfig.MINUTE_DATA.get(deviceId,"a21004");
        NO2.add(Double.valueOf(NO2D));
        DataConfig.MINUTE_DATA.put(deviceId, "a21004", NO2);

        List<Double> CO = DataConfig.MINUTE_DATA.get(deviceId,"a21005")==null?new ArrayList<>():DataConfig.MINUTE_DATA.get(deviceId,"a21005");
        CO.add(Double.valueOf(COD));
        DataConfig.MINUTE_DATA.put(deviceId, "a21005", CO);

        List<Double> O3 = DataConfig.MINUTE_DATA.get(deviceId,"a05024")==null?new ArrayList<>():DataConfig.MINUTE_DATA.get(deviceId,"a05024");
        O3.add(Double.valueOf(O3D));
        DataConfig.MINUTE_DATA.put(deviceId, "a05024", O3);

        List<Double> PM10 = DataConfig.MINUTE_DATA.get(deviceId,"a34002")==null?new ArrayList<>():DataConfig.MINUTE_DATA.get(deviceId,"a34002");
        PM10.add(Double.valueOf(PM10D));
        DataConfig.MINUTE_DATA.put(deviceId, "a34002", PM10);

        List<Double> PM25 = DataConfig.MINUTE_DATA.get(deviceId,"a34004")==null?new ArrayList<>():DataConfig.MINUTE_DATA.get(deviceId,"a34004");
        PM25.add(Double.valueOf(PM25D));
        DataConfig.MINUTE_DATA.put(deviceId, "a34004", PM25);

    }
    public static void createAirMinuteData(String deviceId, StringBuilder dataBlock){
        //SO2
        dataBlock.append(getDataMinute(deviceId,"a21026"));
        //NO2
        dataBlock.append(getDataMinute(deviceId,"a21004"));
        //CO a21005
        dataBlock.append(getDataMinute(deviceId,"a21005"));
        //O3 a05024
        dataBlock.append(getDataMinute(deviceId,"a05024"));
        //PM10 a34002
        dataBlock.append(getDataMinute(deviceId,"a34002"));
        //PM2.5 a34004
        dataBlock.append(getDataMinute(deviceId,"a34004").replace(";",""));

        //处理完毕数据后清空
        DataConfig.MINUTE_DATA.get(deviceId,"a21026").clear();
        DataConfig.MINUTE_DATA.get(deviceId,"a21004").clear();
        DataConfig.MINUTE_DATA.get(deviceId,"a21005").clear();
        DataConfig.MINUTE_DATA.get(deviceId,"a05024").clear();
        DataConfig.MINUTE_DATA.get(deviceId,"a34002").clear();
        DataConfig.MINUTE_DATA.get(deviceId,"a34004").clear();
        log.info("十分钟数据：{}",JSONUtil.toJsonStr(DataConfig.MINUTE_DATA));

    }
    public static void createAirHourData(String deviceId, StringBuilder dataBlock){
        //SO2
        dataBlock.append(getDataHour(deviceId,"a21026"));
        //NO2
        dataBlock.append(getDataHour(deviceId,"a21004"));
        //CO a21005
        dataBlock.append(getDataHour(deviceId,"a21005"));
        //O3 a05024
        dataBlock.append(getDataHour(deviceId,"a05024"));
        //PM10 a34002
        dataBlock.append(getDataHour(deviceId,"a34002"));
        //PM2.5 a34004
        dataBlock.append(getDataHour(deviceId,"a34004").replace(";",""));

        //处理完毕数据后清空
        DataConfig.HOUR_DATA.get(deviceId,"a21026").clear();
        DataConfig.HOUR_DATA.get(deviceId,"a21004").clear();
        DataConfig.HOUR_DATA.get(deviceId,"a21005").clear();
        DataConfig.HOUR_DATA.get(deviceId,"a05024").clear();
        DataConfig.HOUR_DATA.get(deviceId,"a34002").clear();
        DataConfig.HOUR_DATA.get(deviceId,"a34004").clear();
        log.info("小时数据：{}",JSONUtil.toJsonStr(DataConfig.HOUR_DATA));

    }
    public static String getDataHour(String deviceId, String ID){
        List<Double> rtdDataList = DataConfig.HOUR_DATA.get(deviceId,ID);
        //得到和
        double cou = rtdDataList.stream().mapToDouble( Double :: valueOf ).sum();
        //得到平均数
        double avg = rtdDataList.stream().mapToDouble( Double :: valueOf ).average().getAsDouble();
        //得到最小值
        double min = rtdDataList.stream().mapToDouble( Double :: valueOf ).min().getAsDouble();
        //得到最大值
        double max = rtdDataList.stream().mapToDouble( Double :: valueOf ).max().getAsDouble();
        String finalId = ID.replace("-Rtd","");
        String data = finalId+"-Cou="+ getValue(cou) +","+ finalId +"-Min="+  getValue(min) +","+ finalId +"-Avg="+  getValue(avg) +","+ finalId +"-Max="+  getValue(max) +";";
        return data;
    }
    private static String getCellValue(Object sValue){
        String value = sValue.toString()+"";
        if(value.contains(".")) {
            String data = String.format("%.3f", Double.valueOf(value));
            return data;
        }else{
            return value;
        }
    }
    public static String getDataMinute(String deviceId, String ID){
        List<Double> rtdDataList = DataConfig.MINUTE_DATA.get(deviceId,ID);

        List<Double> hourData = DataConfig.HOUR_DATA.get(deviceId,ID)==null?new ArrayList<>():DataConfig.HOUR_DATA.get(deviceId,ID);
        hourData.addAll(rtdDataList);
        DataConfig.HOUR_DATA.put(deviceId,ID,hourData);
        //得到和
        double cou = rtdDataList.stream().mapToDouble( Double :: valueOf ).sum();
        //得到平均数
        double avg = rtdDataList.stream().mapToDouble( Double :: valueOf ).average().getAsDouble();
        //得到最小值
        double min = rtdDataList.stream().mapToDouble( Double :: valueOf ).min().getAsDouble();
        //得到最大值
        double max = rtdDataList.stream().mapToDouble( Double :: valueOf ).max().getAsDouble();
        String finalId = ID.replace("-Rtd","");
        String data = finalId+"-Cou="+ getValue(cou) +","+ finalId +"-Min="+ getValue(min) +","+ finalId +"-Avg="+ getValue(avg) +","+ finalId +"-Max="+ getValue(max) +";";
        return data;
    }
    private static String getValue(double value){
        String v = value+"";
        return getCellValue(v);
    }
}
